//
//  ComposeViewController.swift
//  WBTest
//
//  Created by 张海峰 on 2017/8/1.
//  Copyright © 2017年 张海峰. All rights reserved.
//

import UIKit
import NotificationCenter
class ComposeViewController: UIViewController {
    @IBOutlet weak var picViewCons: NSLayoutConstraint!
    @IBOutlet weak var TextView: ComposeTextView!
    @IBOutlet weak var toolBarCons: NSLayoutConstraint!
    lazy var titleView :ComposeTitleView = ComposeTitleView()
    override func viewDidLoad() {
        super.viewDidLoad()
      setupNavigationBar()
    //监听通知 
    setupNotifictions()
      
    }
   override func viewDidAppear(_ animated: Bool)    {
        super.viewDidAppear(animated)
        TextView.becomeFirstResponder()
    }
   
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}
//设置UI界面
extension ComposeViewController{
    func setupNavigationBar() {
         self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .plain, target: self, action: #selector(closeItemClick))
         self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: .plain, target: self, action: #selector(sendingItemClick))
        navigationItem.rightBarButtonItem?.isEnabled = false
        navigationItem.title = "发微博"
        titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
        navigationItem.titleView = titleView
    }
    func setupNotifictions() {
        //1.监听键盘通知
          NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
        //2.监听添加图片按钮被点击
          NotificationCenter.default.addObserver(self, selector: #selector(addImageBtnClick), name: NSNotification.Name(rawValue: PicPickerAddPhotoNote), object: nil)
    }
}
//添加监听函数
extension ComposeViewController{
    func closeItemClick() {
        dismiss(animated: true, completion: nil)
    }
    func sendingItemClick() {
       print("发送")
    }
    func keyboardWillChangeFrame(note : Notification){
        //弹出键盘隐藏picView
        picViewCons.constant = 0
     //   print(note.userInfo)
        // 获取动画时间
        let duration  = note.userInfo! [UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
        //获取键盘最终的Y值
        let endFrame  = (note.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let y = endFrame.origin.y
        //计算工具栏
        let margin = UIScreen.main.bounds.height - y
        // 执行动画
        toolBarCons.constant = margin
         UIView.animate(withDuration: duration) {
            self.view.layoutIfNeeded()
        }
    }
    @IBAction func imageBtnClick(_ sender: Any) {
       TextView.resignFirstResponder()
       picViewCons.constant = UIScreen.main.bounds.height * 0.65
        UIView.animate(withDuration: 0.25) { 
            self.view.layoutIfNeeded()
        }
    }
    @IBAction func towBtnClick(_ sender: Any) {
        print("towBtnClick")
    }
    @IBAction func thirdBtnClick(_ sender: Any) {
         print("thirdBtnClick")
    }
    @IBAction func smiallBtnClick(_ sender: Any) {
          print("smiallBtnClick")
    }
    @IBAction func plusBtnClick(_ sender: Any) {
          print("plusBtnClick")
    }
    
    
}
//添加添加照片，删除照片的点击事件
extension ComposeViewController{
    func addImageBtnClick(){
        
        //1.判断照片源是否可用
        if !UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            return
        }
        //2.创建照片选择控制器
        let ipc = UIImagePickerController()
        //3.设置照片源
        ipc.sourceType = .photoLibrary
        //4.设置代理
        ipc.delegate = self
        //5.弹出控制器
        present(ipc, animated: true, completion: nil)
    }
}
//添加添加照片，删除照片的点击事件
extension ComposeViewController : UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        //1.获取照片
        let image = info[UIImagePickerControllerOriginalImage] as!UIImage
        //2.展示照片
    }
}
//-UITextView的代理方法
extension ComposeViewController:UITextViewDelegate{
    func textViewDidChange(_ textView: UITextView) {
        self.TextView.placeHolderLabel.isHidden = TextView.hasText
        navigationItem.rightBarButtonItem?.isEnabled = TextView.hasText
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        TextView.resignFirstResponder()
    }
}
